package com.future.mayi.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.fragment.GoodsAllOrderFragment;
import com.future.mayi.utils.KefuUtils;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class GoodsAllOrderActivity extends BaseTitleActivity {
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private List<GoodsAllOrderFragment> fragmentList = new ArrayList<>();
    private static final String[] TITLE = new String[]{"全部", "待付款", "待发货", "待收货", "退款审核中"};
//    private static final String[] TITLE = new String[]{"全部", "待付款", "待发货", "待收货", "退款审核中"};
    private boolean backMain;

    public static void startActivity(Context context, int position) {
        Intent intent = new Intent(context, GoodsAllOrderActivity.class);
        intent.putExtra("position", position);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, int position, boolean backMain) {
        Intent intent = new Intent(context, GoodsAllOrderActivity.class);
        intent.putExtra("position", position);
        intent.putExtra("backMain", backMain);
        context.startActivity(intent);
    }

    @Override
    protected void initTopBar() {
        initBack();
        getRightImg().setImageResource(R.drawable.img_search_order);
        getTitleText().setText("订单");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_goods_all_order);
    }

    @Override
    protected void initView() {
        for (int i = 0; i < TITLE.length; i++) {
            GoodsAllOrderFragment goodsAllOrderFragment = new GoodsAllOrderFragment();
            goodsAllOrderFragment.setStyle(i);
            fragmentList.add(goodsAllOrderFragment);
        }
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });

        initIndicator();
    }

    @Override
    protected void initDates() {
        backMain = getIntent().getBooleanExtra("backMain", false);
        int position = getIntent().getIntExtra("position", 0);

        if (position != 0)
            viewPager.setCurrentItem(position, true);
    }

    @Override
    protected void initListener() {
        getRightImg().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, SearchOrderActivity.class));
            }
        });
    }


    private void initIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return TITLE.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(TITLE[index]);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.text_color4));
                simplePagerTitleView.setPadding(CommonUtils.dip2px(mContext, 22), 0, CommonUtils.dip2px(mContext, 22), 0);
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.black));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.main_color));
                indicator.setRoundRadius(CommonUtils.dip2px(mContext, 2));
                indicator.setLineWidth(CommonUtils.dip2px(mContext, 30));
                indicator.setLineHeight(CommonUtils.dip2px(mContext, 2));
                indicator.setMode(2);
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }


    @Override
    protected void onResume() {
        super.onResume();
//        if (fragmentList != null && fragmentList.size() > 0)
//            fragmentList.get(viewPager.getCurrentItem()).refresh();
    }

    @Override
    public void onBackPressed() {
        if (backMain) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("intentToIndexFragment", true);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        } else super.onBackPressed();
    }
}
